<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-button type="primary">添加角色</el-button>
      <el-table border :data="rolesList">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <div
              class="box"
              v-for="(v, i) in scope.row.children"
              :key="v.id"
              :class="{
                border_bottom_none: i != scope.row.children.length - 1,
              }"
            >
              <div class="box_left">
                <el-tag closable effect="dark"> {{ v.authName }} </el-tag>
              </div>
              <div class="box_right">
                <div
                  class="box_right_item"
                  v-for="(vv, ii) in v.children"
                  :key="vv.id"
                >
                  <div
                    class="box_right_item_left"
                    :class="{ border_bottom_none: ii == v.children.length - 1 }"
                  >
                    <el-tag closable effect="dark" type="success">
                      {{ vv.authName }}
                    </el-tag>
                  </div>
                  <div
                    class="box_right_item_right"
                    :class="{ border_bottom_none: ii == v.children.length - 1 }"
                  >
                    <el-tag
                      closable
                      effect="dark"
                      v-for="(vvv , iii) in vv.children" :key="vvv.id"
                      
                      type="warning"
                    >
                      {{ vvv.authName }}
                    </el-tag>
                  </div>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <el-button type="danger" icon="el-icon-delete" size="mini"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="setRigthsFn(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog title="分配权限" :visible.sync="dialogSetRightsTableVisible">
      <el-tree
        :data="rigthsList"
        show-checkbox
        node-key="id"
        :props="defaultProps"
        default-expand-all
        :default-checked-keys="defaultChecked"
      >
      </el-tree>
    </el-dialog>
  </div>
</template>

<script>
import { getroles } from "@/http/roles.js";
import { getrights } from "@/http/rights.js";

export default {
  data() {
    return {
      rolesList: [],
      dialogSetRightsTableVisible: false,
      rigthsList: [],
      defaultProps: {
        children: "children",
        label: "authName",
      },
      defaultChecked: [],
    };
  },

  mounted() {
    this.getRolesList();
  },

  methods: {
    async getRolesList() {
      let res = await getroles();
      console.log(res);
      this.rolesList = res.data;
    },
    async setRigthsFn(row) {
      this.dialogSetRightsTableVisible = true;
      let res = await getrights("tree");
      this.rigthsList = res.data;

      this.defaultChecked = [];
      this.getCheckId(row, this.defaultChecked);
    },

    getCheckId(row, arr) {
      if (!row.children) {
        arr.push(row.id);
      } else {
        row.children.forEach((v) => this.getCheckId(v, arr));
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  border: 1px solid black;
  display: flex;
}
.box_left {
  width: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-right: 1px solid black;
}
.box_right {
  flex: 1;
}
.box_right_item {
  display: flex;
}
.box_right_item_left {
  width: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-right: 1px solid black;
  border-bottom: 1px solid black;
}
.box_right_item_right {
  flex: 1;
  border-bottom: 1px solid black;
}
.el-tag {
  margin: 15px;
}

.border_bottom_none {
  border-bottom: none;
}
</style>
